package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.IView.IMediaListForPatrolMapForManagerView;
import com.cloudsigns.factorymanager2.engineering.adapter.AddPublicationOrderAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;
import com.cloudsigns.factorymanager2.engineering.bean.PatrolOrderBean;
import com.cloudsigns.factorymanager2.engineering.presenter.MediaListForPatrolMapForManagerPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tony on 2018/6/24.
 */

public class RegionPatrolOrderListActivity extends Activity implements View.OnClickListener, IMediaListForPatrolMapForManagerView {
    private View rl_back;
    private TextView tv_title;
    private TextView tv_region_name;
    private ScrollView scrollview;
    private LinearLayout ll_region;
    private LayoutInflater layoutInflater;
    private String regionId, regionName;
    private String TAG = RegionPatrolOrderListActivity.class.getSimpleName();
    private MediaListForPatrolMapForManagerPresenter mediaListForPatrolMapForManagerPresenter;
    List<MediaBean> mediaBeanList = new ArrayList<>();
    Map<String, List<Map<String, List<MediaBean>>>> mapData = new HashMap<>();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_region_patrol_order_list);

        layoutInflater = getLayoutInflater();
        mediaListForPatrolMapForManagerPresenter = new MediaListForPatrolMapForManagerPresenter(this);
        init();

        Intent mIntent = getIntent();
        if (null != mIntent) {
            regionId = mIntent.getStringExtra(ConstantUtil.KEY_2);
            regionName = mIntent.getStringExtra("regionName");

            tv_region_name.setText(regionName);

        }

        mediaListForPatrolMapForManagerPresenter.getPatrolMediaListForMap();
    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        scrollview = findViewById(R.id.scrollview);
        tv_region_name = findViewById(R.id.tv_region_name);
        ll_region = findViewById(R.id.ll_region);


        tv_title.setText("工单信息");
        rl_back.setOnClickListener(this);


    }

    private void initData() {
        if (null != mediaBeanList) {
            mapData.clear();

            for (MediaBean mediaBean : mediaBeanList) {
                String assignReceiveAccountId = mediaBean.getAssignReceiveAccountId();
                String areaName = mediaBean.getAreaName();

                if (mapData.containsKey(assignReceiveAccountId)) {
                    List<Map<String, List<MediaBean>>> arealist = mapData.get(assignReceiveAccountId);
                    boolean areaIsExit = false;
                    if (null != arealist) {
                        for (Map<String, List<MediaBean>> areaMap : arealist) {
                            if (areaMap.containsKey(areaName)) {
                                areaIsExit = true;
                                List<MediaBean> meidaBeanList = areaMap.get(areaName);
                                meidaBeanList.add(mediaBean);
                            }
                        }

                        if (!areaIsExit) {
                            Map<String, List<MediaBean>> areaMap = new HashMap<>();
                            List<MediaBean> meidaBeanList = new ArrayList<>();
                            meidaBeanList.add(mediaBean);

                            areaMap.put(areaName, meidaBeanList);
                            arealist.add(areaMap);
                        }

                    }
                } else {
                    List<Map<String, List<MediaBean>>> arealist = new ArrayList<>();
                    Map<String, List<MediaBean>> areaMap = new HashMap<>();
                    List<MediaBean> meidaBeanList = new ArrayList<>();

                    meidaBeanList.add(mediaBean);
                    areaMap.put(areaName, meidaBeanList);
                    arealist.add(areaMap);

                    mapData.put(assignReceiveAccountId, arealist);
                }
            }


        }

        updateUi();

    }

    private void updateUi() {
        if (null != mapData && null != ll_region) {
            ll_region.removeAllViews();

            for (String assignReceiveAccountId : mapData.keySet()) {
                List<Map<String, List<MediaBean>>> arealist = mapData.get(assignReceiveAccountId);
                View assignReceiveAccountId_view = layoutInflater.inflate(R.layout.item_region_patrol_order_list, null);
                TextView tv_assignReceiveAccountName = assignReceiveAccountId_view.findViewById(R.id.tv_assignReceiveAccountName);
                LinearLayout ll_areaName_list = assignReceiveAccountId_view.findViewById(R.id.ll_areaName_list);
                ll_areaName_list.removeAllViews();

                tv_assignReceiveAccountName.setText(assignReceiveAccountId);

                if (null != arealist) {
                    for (int i = 0; i < arealist.size(); i++) {
                        Map<String, List<MediaBean>> areaMap = arealist.get(i);
                        View view_area = layoutInflater.inflate(R.layout.item_region_patrol_order_area, null);
                        TextView tv_area_name = view_area.findViewById(R.id.tv_area_name);
                        ListView lv_media = view_area.findViewById(R.id.lv_media);

                        for (String key : areaMap.keySet()) {
                            tv_area_name.setText(key);
                            List<MediaBean> meidaBeanList = areaMap.get(key);
                            if (null != meidaBeanList) {
                                AddPublicationOrderAdapter adapter = new AddPublicationOrderAdapter(this, TAG);
                                adapter.setData(meidaBeanList);
                                lv_media.setAdapter(adapter);
                            }
                        }

                        ll_areaName_list.addView(view_area);

                    }

                }

                ll_region.addView(assignReceiveAccountId_view);
            }
        }

        scrollview.smoothScrollTo(0, 0);


    }

    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;

        }
    }


    @Override
    public String getId() {
        return regionId;
    }

    @Override
    public void getPatrolMediaListForMapSuccessed(List<MediaBean> beanList) {
        if (null != beanList) {
            mediaBeanList.clear();
            mediaBeanList.addAll(beanList);

            initData();
        }
    }

    @Override
    public void showFailedError(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
